package com.sokal.easycare.modules.position.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sokal.easycare.common.config.Global;
import com.sokal.easycare.common.persistence.Page;
import com.sokal.easycare.common.web.BaseController;
import com.sokal.easycare.common.utils.StringUtils;
import com.sokal.easycare.modules.position.entity.Duty;
import com.sokal.easycare.modules.position.service.DutyService;

/**
 * 岗位Controller
 * @author yangmin
 * @version 2017-04-08
 */
@Controller
@RequestMapping(value = "${adminPath}/position/duty")
public class DutyController extends BaseController {

	@Autowired
	private DutyService dutyService;
	
	@ModelAttribute
	public Duty get(@RequestParam(required=false) String id) {
		Duty entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = dutyService.get(id);
		}
		if (entity == null){
			entity = new Duty();
		}
		return entity;
	}
	
	@RequiresPermissions("position:duty:view")
	@RequestMapping(value = {"list", ""})
	public String list(Duty duty, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Duty> page = dutyService.findPage(new Page<Duty>(request, response), duty); 
		model.addAttribute("page", page);
		return "modules/position/dutyList";
	}

	@RequiresPermissions("position:duty:view")
	@RequestMapping(value = "form")
	public String form(Duty duty, Model model) {
		model.addAttribute("duty", duty);
		return "modules/position/dutyForm";
	}

	@RequiresPermissions("position:duty:edit")
	@RequestMapping(value = "save")
	public String save(Duty duty, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, duty)){
			return form(duty, model);
		}
		dutyService.save(duty);
		addMessage(redirectAttributes, "保存岗位成功");
		return "redirect:"+Global.getAdminPath()+"/position/duty/?repage";
	}
	
	@RequiresPermissions("position:duty:edit")
	@RequestMapping(value = "delete")
	public String delete(Duty duty, RedirectAttributes redirectAttributes) {
		dutyService.delete(duty);
		addMessage(redirectAttributes, "删除岗位成功");
		return "redirect:"+Global.getAdminPath()+"/position/duty/?repage";
	}

}